//
//  KKHttpFileManager.m
//  DecxForward
//
//  Created by 程强 on 16/11/1.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "KKHttpFileManager.h"


@implementation KKHttpFileManager
+ (void)httpUpload:(NSString *)url key:(NSString *)key parameter:(NSDictionary *)dic withData:(NSData *)data useName:(NSString *)fileName mimeType:(NSString *)style success:(KKSuccessBlock)succsssBlock failure:(KKFailureBlock)failureBlock
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:url parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:data name:key fileName:fileName mimeType:style];
 
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *result  =[[ NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSMutableDictionary* jsonDic = nil;
        if (responseObject) {
            jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        }
        KKHttpParse *parse = [[KKHttpParse alloc]init];
        parse.responseCode = [jsonDic[@"status"] integerValue];
        parse.responseMsg = jsonDic[@"message"];
        parse.responseJsonOB = jsonDic;
        
        if (parse) {
            if (parse.responseCode == 1) {
                succsssBlock(parse.responseJsonOB,parse);
            }else{
                //逻辑错误
                NSString *msg = parse.responseMsg;
                [parse.responseHead setValue:msg forKey:@"message"];
                failureBlock(parse);
            }
        }
        KKLog(@"接口返回的json%@",result);
        KKLog(@"上传成功！上传日志%@",result);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        KKLog(@"上传失败！");
        KKHttpParse *a = [[KKHttpParse alloc] init];
        a.responseCode = 1000;
        a.responseMsg = MESSAGE_NoNetwork;
        [a.responseHead setValue:MESSAGE_NoNetwork forKey:@"message"];
        failureBlock(a);
    }];
}
+ (void)uploadImage:(NSString *)url parameter:(NSDictionary *)dic withImageData:(NSData *)image imageName:(NSString *)name imageType:(NSString *)type success:(KKSuccessBlock)sucBlock failure:(KKFailureBlock)failureBlock
{
    NSString *mineType = [NSString stringWithFormat:@"image/%@",type];
    [[self class]httpUpload:url key:@"image" parameter:dic withData:image useName:name mimeType:mineType success:^(id responseObject, KKHttpParse *parse) {
        sucBlock(responseObject,parse);
    } failure:^(KKHttpParse *parse) {
        failureBlock(parse);
    }];
}
+ (void)uploadVideo:(NSString *)url parameter:(NSDictionary *)dic withVideoData:(NSData *)image videoName:(NSString *)name success:(KKSuccessBlock)sucBlock failure:(KKFailureBlock)failureBlock
{
    [[self class]httpUpload:url key:@"image" parameter:dic withData:image useName:name mimeType:@"video/quicktime" success:^(id responseObject, KKHttpParse *parse) {
        sucBlock(responseObject,parse);
    } failure:^(KKHttpParse *parse) {
        failureBlock(parse);
    }];
}
@end
